﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnimationViewControls
{
    /// <summary>
    /// AnimationManagerView.xaml 的交互逻辑
    /// </summary>
    public partial class AnimationManagerView : UserControl
    {
        public ObservableCollection<TestModel> TestModels { get; set; }
        public AnimationManagerView()
        {
            TestModels = new ObservableCollection<TestModel>();
            TestModels.Add(new TestModel { Name = "item1" });
            TestModels.Add(new TestModel { Name = "item2" });
            TestModels.Add(new TestModel { Name = "item3" });
            TestModels.Add(new TestModel { Name = "item4" });
            TestModels.Add(new TestModel { Name = "item5" });
            InitializeComponent();
            
        }

        
        private parentItem FindVisualParent<parentItem>(DependencyObject obj) where parentItem:DependencyObject
        {
            obj=VisualTreeHelper.GetParent(obj);
            if (obj!=null&&obj is parentItem)
            {
                return (parentItem)obj;
            }
            else
            {
                obj = FindVisualParent<parentItem>(obj);
                if (obj!=null)
                {
                    return (parentItem)obj;
                }
            }
            return null;
        }

        public childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
        {
            // Search immediate children first (breadth-first)
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);

                if (child != null && child is childItem)
                    return (childItem)child;

                else
                {
                    childItem childOfChild = FindVisualChild<childItem>(child);

                    if (childOfChild != null)
                        return childOfChild;
                }
            }

            return null;
        }
        ListViewItem SelectItemContainer = null;
        Grid SelectedPanal=null;
        StackPanel ItemsPanel = null;
        bool ItemMoveStatus = false;
        Point ItemOriginPoint;
        MoveAdorner MoveAdorner = null;
        private void Drag_container(object sender, MouseButtonEventArgs e)
        {
            
            if(e.ButtonState==MouseButtonState.Pressed)
            {
                var aa=HitTest(this.DragableListView, e.GetPosition(this.DragableListView));
                //var dataContext = ((ListItem)sender).DataContext as TestModel;
                SelectItemContainer=FindVisualParent<ListViewItem>(sender as DependencyObject);
                SelectedPanal = FindVisualChild<Grid>(SelectItemContainer);
                //TestModels.Remove(dataContext);
                if (SelectItemContainer==null)
                {
                    return;
                }
                ItemsPanel = FindVisualChild<StackPanel>(this.DragableListView);
                if (ItemsPanel==null)
                {
                    return;
                }
                AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(ItemsPanel);
                if (adornerLayer==null)
                {
                    return;
                }
                MoveAdorner = new MoveAdorner(ItemsPanel as UIElement);
                MoveAdorner.AddElement(SelectedPanal);
                adornerLayer.Add(MoveAdorner);
                var cursorPoint = e.GetPosition(ItemsPanel);
                ItemMoveStatus = true;
                Console.WriteLine($"x:{cursorPoint.X},y:{cursorPoint}");
                ItemOriginPoint = new Point(cursorPoint.X, cursorPoint.Y);
                //TestModels.Remove(dataContext);
           
                
            }
        }

    
        private void Move_itemHandler(object sender, MouseEventArgs e)
        {
            if (ItemMoveStatus==true&&ItemsPanel!=null)
            {
                var mousePoint = e.GetPosition(ItemsPanel);
                Panel.SetZIndex(SelectedPanal, 9999);
                var offsetY = mousePoint.Y - ItemOriginPoint.Y;
                DataObject obj = new DataObject(typeof(ListViewItem),SelectedPanal);
                DragDrop.DoDragDrop(this.SelectedPanal, obj, DragDropEffects.Copy);
                //SelectedPanal.Background = new SolidColorBrush(Colors.Red);
                //ItemsPanel.Margin = new Thickness(0, 1000, 0, 0);
                //SelectItemContainer.Width += offsetY;
                //Console.WriteLine(originMargin.Top);
            }
        }

        private bool HitTest(DependencyObject obj,Point hPoint)
        {
            var visualObj = obj as Visual;
            if (visualObj==null)
            {
                return false;
            }
            var result=VisualTreeHelper.HitTest(visualObj, hPoint);
            return false;

        }
        private void StopDrag_Container(object sender, MouseButtonEventArgs e)
        {
            ItemMoveStatus = false;
        }


    }
    public class TestModel
    {
        public string Name { get; set; }
    }
}
